package cn.edu.szh.dao;

import cn.edu.szh.bean.ScoreInputInfo;
import cn.edu.szh.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ScoreInputDao {
    public boolean add(List<ScoreInputInfo> info){
        Connection conn = DBUtil.getConnection();
        try{
            String sql = "insert into score_use values(?,?,?,?,?,?,?,?)";
            conn.setAutoCommit(false);
            PreparedStatement ps = conn.prepareStatement(sql);
            for(ScoreInputInfo scoreInputInfo : info){
                ps.setInt(1, scoreInputInfo.getGrade_id());
                ps.setInt(2, scoreInputInfo.getClass_id());
                ps.setInt(3, scoreInputInfo.getStudent_id());
                ps.setString(4, scoreInputInfo.getName());
                ps.setInt(5, scoreInputInfo.getSubject_id());
                ps.setString(6, scoreInputInfo.getSname());
                ps.setDouble(7, scoreInputInfo.getScore());
                ps.setString(8, scoreInputInfo.getTerm());
                ps.execute();
            }
            conn.commit();
            return true;
        }catch (Exception e){
            try{
                conn.rollback();
            }catch (Exception e1){
                throw new RuntimeException(e1);
            }
            e.printStackTrace();
        }
        return false;
    }
//    public List<MasterUse> findByMasterAndDate(Master_info masterInfo,String month) {
//        ArrayList<MasterUse> result = new ArrayList<>();
//        Connection conn = DBUtil.getConnection();
//        try {
//            conn = DBUtil.getConnection();
//            PreparedStatement ps = conn.prepareStatement("select * from master_use where district_id=? and building_id=? and room_id=? and date=?");
//            ps.setInt(1, masterInfo.getDistrict_id());
//            ps.setInt(2, masterInfo.getBuilding_id());
//            ps.setInt(3, masterInfo.getRoom_id());
//            ps.setString(4, month);
//            ResultSet rs = ps.executeQuery();
//            while (rs.next()) {
//                MasterUse infoTemp = new MasterUse();
//                infoTemp.setDistrict_id(rs.getInt("district_id"));
//                infoTemp.setBuilding_id(rs.getInt("building_id"));
//                infoTemp.setRoom_id(rs.getInt("room_id"));
//                infoTemp.setDate(rs.getString("date"));
//                infoTemp.setWater_reading(rs.getDouble("water_reading"));
//                infoTemp.setElec_reading(rs.getDouble("elec_reading"));
//                infoTemp.setGas_reading(rs.getDouble("gas_reading"));
//                result.add(infoTemp);
//            }
//            return result;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            System.out.println("查询用户失败");
//        }
//        return result;
//    }
}
